using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Web.UI;
using Resources;
using mojoPortal.Business.WebHelpers;
using ud_Article.Business;
using ud_Utilities;
using System.Collections.Generic;
using System.Text;

namespace ud_Article.UI
{

    public partial class ArticleSearchModule : SiteModuleControl
    {
        protected ModuleTitleControlCustom TitleControl;
        protected ArticleSearchConfiguration config = new ArticleSearchConfiguration();
        #region OnInit

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (Site != null && Site.DesignMode) { return; }
            Load += Page_Load;
            btnSearch.Click += btnSearch_Click;
            ddlModules.SelectedIndexChanged += ddlModules_SelectedIndexChanged;
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.EnableViewState = true;
            if (Site != null && Site.DesignMode) { return; }
            if (Page.IsPostBack) return;
            LoadSettings();
            SetupJqueryScript();
            PopulateLabels();
            PopulateControls();
        }

        private void SetupJqueryScript()
        {
            if (!config.UseExpandMode) return;
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>");
            sb.Append("$(document).ready(function(){");
            sb.Append("$('.spanexpand').attr('title','" + config.ExpandText + "');");
            sb.Append("$('.expandtrigger').toggle(function(){");
            sb.Append("$('.advancesearch').slideToggle();");
            sb.Append("},function(){");
            sb.Append("$('.advancesearch').slideToggle();");
            sb.Append("});");
            sb.Append("});");
            sb.Append("</script>");
            if (!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "search" + ModuleId.ToInvariantString()))
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "search" + ModuleId.ToInvariantString(), sb.ToString());
            }
        }

        private void PopulateLabels()
        {
            txtSearchInput.Watermark = config.SearchWatermarkTextSettings;
            btnSearch.Text = config.SearchButtonText;
            lblSearch.Text = config.SearchLabelText == string.Empty ? string.Empty : config.SearchLabelText;
            Image imgProgress = upProgress1.FindControl("imgProgress") as Image;
            if (imgProgress == null) return;
            imgProgress.ImageUrl = ImageSiteRoot + "/Data/Icon16x16/updateprogress.gif";
            imgProgress.AlternateText = ArticleResources.Loading;
        }

        private void LoadSettings()
        {
            config = new ArticleSearchConfiguration(Settings);
            siteSettings = CacheHelper.GetCurrentSiteSettings();
            pnlContainer.ModuleId = ModuleId;
            if (config.InstanceCssClass != string.Empty)
            {
                pnlContainer.CssClass += " " + config.InstanceCssClass;
            }
            pnlExpandButton.Visible = config.UseExpandMode;
            if (config.ExpandText != string.Empty)
            {
                ltrExpandText.Text = config.ExpandText;
            }
            else
            {
                pnlExpandText.Visible = false;
            }
        }

        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            BindModules();
            BindCategories();
            BindSearchType();
        }

        void ddlModules_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCategories();
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            config = new ArticleSearchConfiguration(Settings);
            if (txtSearchInput.Text.Trim() == config.SearchWatermarkTextSettings)
            {
                txtSearchInput.Text = string.Empty;
            }
            Session["searchterm"] = txtSearchInput.Text.Trim();
            
            WebUtils.SetupRedirect(this, SiteRoot + "/ArticleLoader/SearchResult.aspx?pageid=" + PageId.ToInvariantString() + "&mid=" + ModuleId.ToInvariantString() + "&sm=" + ddlModules.SelectedValue + "&sc=" + ddlCategories.SelectedValue + "&st=" + ddlSearchType.SelectedValue);
        }

        private void BindSearchType()
        {
            ddlSearchType.Items.Add(new ListItem(ArticleResources.ArticleSearchAll, "0"));
            ddlSearchType.Items.Add(new ListItem(ArticleResources.ArticleSearchTitle, "1"));
            ddlSearchType.Items.Add(new ListItem(ArticleResources.ArticleSearchContent, "2"));
        }

        private void BindCategories()
        {
            DBArticleLinq repository = new DBArticleLinq();
            int mid = int.Parse(ddlModules.SelectedValue);
            List<ud_ArticleCategory> rootItems = repository.GetCategories(mid);
            ddlCategories.DataSource = rootItems;
            ddlCategories.DataTextField = "Category";
            ddlCategories.DataValueField = "CategoryID";
            ddlCategories.DataBind();
            ddlCategories.Items.Insert(0, new ListItem(ArticleResources.ArticleSearchAll, "-1"));
        }

        private void BindModules()
        {
            DBUtilities repository = new DBUtilities();
            ddlModules.DataSource = repository.GetModuleByModuleGuid(new Guid("7c4ad96d-ef7b-4120-bcb6-1233b87407ef"), siteSettings.SiteId);
            ddlModules.DataTextField = "ModuleTitle";
            ddlModules.DataValueField = "ModuleID";
            ddlModules.DataBind();
            ddlModules.Items.Insert(0, new ListItem(ArticleResources.ArticleSearchAll, "-1"));
        }
    }
}